﻿using Models;
using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using strange.extensions.mediation.impl;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace CommonIoC
{
    public class PersonOneZhuangBeiMediator : Mediator
    {

       

        [Inject]
        public PersonOneZhuangBeiView view { get; set; }
        [Inject(ContextKeys.CONTEXT_DISPATCHER)]//全局的dispatcher
        public IEventDispatcher dispatcher { get; set; }

        public override void OnRegister()
        {

             dispatcher.AddListener( PersonZhuangBeiBagEvent.ClickOneItem  , onShowOneItem );
            //  dispatcher.AddListener(PlayerGameState.CanMove_And_CanTouchWSAD, onCanMove_And_CanTouchWSAD);
          //  dispatcher.AddListener(TopButtonListEvent.ClickHelper, onShowHelperPanel );


         //   dispatcher.AddListener(TopButtonListEvent.ClickBack, onTopButtonListClickBack);

            AddBackButtonClickFunc();
            AddZhuangBeiButtonClickFunc();
            AddDropItemButtonClickFunc();
        }

        private void onShowOneItem(IEvent payload)
        {
            int text_id = Convert.ToInt32(payload.data);
            Debug.Log("传递过来的 oneItem值为："+ text_id );
            ShowOneItemInfo(text_id);

            view.ShowThisUI();
            //读取装备的操作。。。
        }

        void ShowOneItemInfo( int  id )
        {
            tempid = id;

           // Dictionary<string, GamePerson> allplayer = GameDataManager.Instance.GetPersonDic();

            GamePerson person = GameDataManager.Instance.GetGamePerson();

            PersonZhuangBeiItem item = person.zb_bag.bag[id];

            view.itemName.text = item.Name;

           
            view.itemType.text = string.Format("类型:{0}", GetItemTypeString(item.type));

            view.itemAddAttack.text = string.Format("攻击:{0}", item.Attack);
            view.itemAddFangWei.text = string.Format("防卫:{0}", item.FangYu );

        }

        int tempid;

        void SetZhuangBei()
        {
           // Dictionary<string, GamePerson> allplayer = GameDataManager.Instance.GetPersonDic();

            GamePerson person = GameDataManager.Instance.GetGamePerson();

            PersonZhuangBeiItem item = person.zb_bag.bag[tempid];

            //这里 可以是装备 或者 是 卸载装备
            item.ZhuangBei();
            //检查 这个物品同类 有没有 已经装备的，如果有，将之前已经装备的 状态去掉


            foreach (KeyValuePair<int, PersonZhuangBeiItem> kv in person.zb_bag.bag)
            {

                if ( kv.Value != item &&  kv.Value.type == item.type && kv.Value.isZhuangBei)
                {
                    kv.Value.isZhuangBei = false;
                }

            }




            
        }

        void DropItem()
        {
            GamePerson person = GameDataManager.Instance.GetGamePerson();

            person.zb_bag.bag.Remove(tempid);
           // PersonZhuangBeiItem item = allplayer["1"].zb_bag.bag[tempid];

            // item.ZhuangBei();
        }


        string GetItemTypeString( PersonZhuangBeiType  _t)
        {
            if( _t == PersonZhuangBeiType.Wuqi )
            {
                return "武器";
            }

            else if (_t == PersonZhuangBeiType.Kuzi)
            {
                return "裤子";
            }
            else if (_t == PersonZhuangBeiType.Yifu)
            {
                return "衣服";
            }
            else 
            {
                return "鞋子";
            }
        }

        void AddBackButtonClickFunc()
        {
            if (view.btn_back != null)
            {
                view.btn_back.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch(  PersonOneZhuangBeiViewEvent.ClickBack  );
                    view.CloseThisUI();
                });
            }
        }

        void AddZhuangBeiButtonClickFunc()
        {
            if (view.btn_Set_zhuangBeiThis != null)
            {
                view.btn_Set_zhuangBeiThis.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch(PersonOneZhuangBeiViewEvent.ClickBack);
                    SetZhuangBei();

                    view.CloseThisUI();
                });
            }
        }

        void AddDropItemButtonClickFunc()
        {
            if (view.btn_drop != null)
            {
                view.btn_drop.onClick.AddListener(delegate ()
                {

                    dispatcher.Dispatch(PersonOneZhuangBeiViewEvent.ClickBack);
                    DropItem();

                    view.CloseThisUI();
                });
            }
        }

        public override void OnRemove()
        {
            //dispatcher.RemoveListener(PlayerStageCommandEvent.canMove, onPlayerStageChangeTocanMove);
            //dispatcher.RemoveListener(PlayerStageCommandEvent.doTalk, onPlayerStageChangeTodoTalk);

            //view.dispatcher.RemoveListener(GameEvent.InputKey, onInputKey);
        }


    }
}
